package com.view.resourcemanage.CustomerFrame;

import com.bean.Customer;
import com.bean.User;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoTable;
import com.service.CustomerService;
import com.service.impl.CustomerServiceImpl;
import com.util.PageUtilSuper;
import com.view.MainFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class ClientManagePanel extends JPanel {
    private LovoTable cilTab;

    private LovoButton cilInBtn;
    private LovoButton cilChgBtn;
    private LovoButton cilInfoBtn;

    private JPanel queryPanel;
    private JLabel nameLab;
    private JLabel rangeLab;
    private JTextField nameTxt;
    private JTextField rangeTxt;
    private JButton queryBtn;

    private MainFrame frame;

    private LovoButton prev,next;

    private JLabel txtPage;
    private PageUtilSuper<Customer> pageUtilSuper;
    private int pgNumber = 1;
    private int pageSize = 3;

    private CustomerService cus = new CustomerServiceImpl();

    public ClientManagePanel(MainFrame frame) {
        this.frame = frame;
        this.setBackground(Color.WHITE);
        this.setLayout(null);
        setPageUtilSuper();
        this.initTable();
        this.initBtn();
        this.initQueryPanel();
        //this.bindTable();
        txtPage = new JLabel();
        this.txtPage.setBounds(170, 305, 100, 50);
        this.add(this.txtPage);
        labelInit();
    }

    private void labelInit() {
        int maxPage = pageUtilSuper.getMaxNumber();
        this.txtPage.setText(pgNumber+" / "+maxPage);
    }

    public void setPageUtilSuper() {
        List<Customer> customers = new ArrayList<>();
        customers = cus.getAllCustomer();
        pageUtilSuper = new PageUtilSuper(pageSize,customers);
    }

    public void flush(){
        tableFlush();
        labelInit();
    }

    public void tableFlush(){
        setPageUtilSuper();
        List<Customer> customers = new ArrayList<>();
        customers = pageUtilSuper.getPgNumberThing(pgNumber);
        cilTab.updateLovoTable(customers);
    }

    private void initTable() {
        // TODO Auto-generated method stub
        this.cilTab = new LovoTable(this, new String[] { "客户名称", "负责人", "经营范围"
        }, new String[] {"name","director","manageRange"},// 数组中应该是对应属性的属性名
                "id");// 填入唯一标示属性
        List<Customer> customers = new ArrayList<>();
        customers = pageUtilSuper.getPgNumberThing(1);
        cilTab.updateLovoTable(customers);
        this.cilTab.setSizeAndLocation(20, 0, 750, 300);

    }

    private void initBtn(){
        this.cilInBtn = new LovoButton("添加客户", 30, 380, this);
        this.cilChgBtn = new LovoButton("修改客户信息", 250, 380, this);
        this.cilInfoBtn = new LovoButton("查看客户信息", 30, 450, this);

        this.prev = new LovoButton("上一页",30,320,this);
        this.next = new LovoButton("下一页",250,320,this);

        cilInBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CustomerAddDialog addDialog = new CustomerAddDialog(frame);
            }
        });

        cilChgBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int key = cilTab.getKey();
                if(key == -1){
                    JOptionPane.showMessageDialog(null,"请先选择一行");
                }else {
                    CustomerUpdateDialog customerUpdateDialog = new CustomerUpdateDialog(frame,key);
                }
            }
        });

        cilInfoBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int key = cilTab.getKey();
                if(key == -1){
                    JOptionPane.showMessageDialog(null,"请先选择一行");
                }else {
                    CustomerShowDialog customerShowDialog = new CustomerShowDialog(frame,key);
                }
            }
        });

        prev.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (pageUtilSuper.havePrev(pgNumber)) {
                    pgNumber--;
                    flush();
                } else {
                    JOptionPane.showMessageDialog(null, "当前是第一页");
                }
            }
        });

        next.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (pageUtilSuper.haveNext(pgNumber)) {
                    pgNumber ++;
                    flush();
                } else {
                    JOptionPane.showMessageDialog(null, "当前是最后一页");
                }
            }
        });





    }

    private void initQueryPanel() {
        // TODO Auto-generated method stub
        this.queryPanel = new JPanel();
        this.queryPanel.setBorder(BorderFactory.createTitledBorder("查询供应商信息"));
        this.queryPanel.setBackground(Color.WHITE);
        this.queryPanel.setLayout(null);
        this.queryPanel.setBounds(380, 310, 300, 180);
        this.add(this.queryPanel);

        // 标签
        this.nameLab = new JLabel("客户名称");
        this.nameLab.setBounds(40, 30, 50, 30);
        this.queryPanel.add(this.nameLab);
        this.rangeLab = new JLabel("经营范围");
        this.rangeLab.setBounds(40, 70, 50, 30);
        this.queryPanel.add(this.rangeLab);


        // 文本框
        this.nameTxt = new JTextField();
        this.nameTxt.setBounds(100, 37, 130, 20);
        this.queryPanel.add(this.nameTxt);
        this.rangeTxt = new JTextField();
        this.rangeTxt.setBounds(100, 77, 130, 20);
        this.queryPanel.add(this.rangeTxt);


        // 查询按钮
        this.queryBtn = new JButton("查询");
        this.queryBtn.setBounds(200, 140, 80, 20);
        this.queryPanel.add(this.queryBtn);

        queryBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String nameLabText = nameTxt.getText();
                String rangeLabText = rangeTxt.getText();
                List<Customer> customers = new ArrayList<>();
                customers = cus.findCustomer(nameLabText,rangeLabText);
                System.out.println(customers);
                cilTab.updateLovoTable(customers);
            }
        });
    }
}
